package com.ruoyi.ResearchProjects.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.DegreePoints.mapper.DegreePointsMapper;
import com.ruoyi.ResearchProjects.mapper.ResearchProjectsMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ResearchProjects.domain.ResearchProjects;
import com.ruoyi.ResearchProjects.service.IResearchProjectsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 科研项目Controller
 * 
 * @author ruoyi
 * @date 2025-04-13
 */
@RestController
@RequestMapping("/ResearchProjects/ResearchProjects")
public class ResearchProjectsController extends BaseController
{
    @Autowired
    private IResearchProjectsService researchProjectsService;

    @Autowired
    private ResearchProjectsMapper researchProjectsMapper;

    @Autowired
    private DegreePointsMapper degreePointsMapper;

    /**
     * 查询科研项目列表
     */
    @PreAuthorize("@ss.hasPermi('ResearchProjects:ResearchProjects:list')")
    @GetMapping("/list")
    public TableDataInfo list(ResearchProjects researchProjects)
    {
        startPage();
        researchProjects.setPointId(getLoginUser().getUsername().equals("admin")? 0 : getLoginUser().getUserId());
        List<ResearchProjects> list = researchProjectsService.selectResearchProjectsList(researchProjects);
//        if(list.size() != 0 && getLoginUser().getUser().getRemark().equals("管理员")){
//            for (int i = 0; i < list.size(); i++) {
//                list.get(i).setPointName(degreePointsMapper.selectDegreePointsByPointId(list.get(i).getPointId()).getPointName());
//
//            }
//
//        }
        return getDataTable(list);
    }

    //查询专家审核科研项目列表
    @GetMapping("/byExpert")
    public TableDataInfo listByExpert()
    {
        startPage();
        List<ResearchProjects> list = researchProjectsMapper.selectByExpert(getLoginUser().getUserId());
        if(list.size() != 0){
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setPointName(degreePointsMapper.selectDegreePointsByPointId(list.get(i).getPointId()).getPointName());

            }
        }
        return getDataTable(list);
    }

    /**
     * 导出科研项目列表
     */
    @PreAuthorize("@ss.hasPermi('ResearchProjects:ResearchProjects:export')")
    @Log(title = "科研项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ResearchProjects researchProjects)
    {
        List<ResearchProjects> list = researchProjectsService.selectResearchProjectsList(researchProjects);
        ExcelUtil<ResearchProjects> util = new ExcelUtil<ResearchProjects>(ResearchProjects.class);
        util.exportExcel(response, list, "科研项目数据");
    }

    /**
     * 获取科研项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('ResearchProjects:ResearchProjects:query')")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") Long projectId)
    {
        return success(researchProjectsService.selectResearchProjectsByProjectId(projectId));
    }

    /**
     * 新增科研项目
     */
    @PreAuthorize("@ss.hasPermi('ResearchProjects:ResearchProjects:add')")
    @Log(title = "科研项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResearchProjects researchProjects)
    {
        researchProjects.setPointId(getLoginUser().getUserId());
        return toAjax(researchProjectsService.insertResearchProjects(researchProjects));
    }

    /**
     * 修改科研项目
     */
    @PreAuthorize("@ss.hasPermi('ResearchProjects:ResearchProjects:edit')")
    @Log(title = "科研项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResearchProjects researchProjects)
    {
        return toAjax(researchProjectsService.updateResearchProjects(researchProjects));
    }

    /**
     * 删除科研项目
     */
    @PreAuthorize("@ss.hasPermi('ResearchProjects:ResearchProjects:remove')")
    @Log(title = "科研项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Long[] projectIds)
    {
        return toAjax(researchProjectsService.deleteResearchProjectsByProjectIds(projectIds));
    }
}
